package www.qiyue.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * 简单封装FastJson的使用
 * @author win
 *
 */
public class JsonUtilz {
    public static <T> T parse(String jsonString, Class<T> clazz) {
    	return parse(jsonString, clazz, true);
    }
    
    public static <T> T parse(String jsonString, Class<T> clazz, boolean silent) {
   		try {
			return JSON.parseObject(jsonString, clazz);
		} catch (Exception e) {
			if (silent == false) throw e;
			return null;
		}
    }
    
    public static <T> List<T> parseList(String jsonString, Class<T> clazz) {
   		return JSON.parseArray(jsonString, clazz);
    }
    
    public static String toString(Object obj, boolean pretty) {
    	try {
			return JSON.toJSONString(obj, pretty);
		} catch (Exception e) {
			return "";
		}
    }
    
    public static JSONObject parseObject(String jsonString) {
		try {
			return JSON.parseObject(jsonString);
		} catch (Exception e) {
			return new JSONObject();
		}
    }
    
    public static JSONArray parseArray(String jsonString) {
		try {
			return JSON.parseArray(jsonString);
		} catch (Exception e) {
			return new JSONArray();
		}
    }
    
    
    
}
